<template>
	<view class="container">

	</view>
</template>


<script>
	import baseUtil from "../../utils/baseUtil.js"

	export default {
		data() {
			return {
				payState: 0, // 0 未支付 1 支付成功 2 支付失败
				title: "立即支付完成订单",
				action: "", //意图 pay orderPay
				epPayData: {
					payUrl: "",
					requestNo: "",
					payToken: ""
				}
			}
		},
		onLoad(options) {
			this.action = options.action;
			this.epPayData = {
				payUrl: decodeURIComponent(baseUtil.baseDecode(options.payUrl)),
				requestNo: options.requestNo,
				payToken: options.payToken
			}
			this.topay()
		},
		methods: {
			topay() {
				if (this.action == "epPay") {
					this.epPay(this.epPayData);
				} else {
					uni.showModal({
						title: "提示",
						content: "不支持的支付方式"
					})
					this.payState = 2
					this.title = "支付失败"
				}
			},
			//快捷通
			epPay(requestParam) {
				if (baseUtil.isEmpty(requestParam.payUrl) || baseUtil.isEmpty(requestParam.requestNo) || baseUtil.isEmpty(
						requestParam.payToken)) {
					uni.showModal({
						title: "提示",
						content: "缺少必要参数"
					})
					this.payState = 2
					this.title = "支付失败"
					return;
				}
				var that = this;
				uni.showToast({
					title: '登录中',
					icon: 'loading',
					duration: 20000,
					mask: true,
				})
				uni.login({
					success(res) {
						var code = res.code;
						wx.hideToast({
							success: (res) => {},
						})
						wx.showToast({
							title: '支付中，请稍后',
							icon: 'loading',
							duration: 20000,
							mask: true,
						})
						console.log(requestParam.payUrl + "?js_code=" + code);

						uni.request({
							url: requestParam.payUrl,
							method: "POST",
							data: {
								code: code,
								requestNo: requestParam.requestNo,
								payToken: requestParam.payToken
							},
							success(res) {
								uni.hideToast()
								console.log("请求支付返回状态:" + res.data.status)
								that.pay(res.data.timeStamp, res.data.nonceStr, res.data
									.packageStr, res.data.signType, res.data.paySign);
							},
							fail() {
								uni.hideToast()
								that.payFail();
							}
						})
					},
					fail() {
						that.payFail()
					}
				})
			},

			//对已下单的订单进行支付
			pay: function(timeStamp, nonceStr, pack, signType, paySign) {
				if (baseUtil.isEmpty(timeStamp) || baseUtil.isEmpty(nonceStr) || baseUtil.isEmpty(pack) || baseUtil
					.isEmpty(
						signType) || baseUtil.isEmpty(paySign)) {
					uni.showModal({
						title: "提示",
						content: "缺少必要参数"
					})
					this.payState = 2
					this.title = "支付失败"
					return;
				}
				var that = this;
				uni.showToast({
					title: '支付中，请稍后',
					icon: 'loading',
					duration: 2000,
					mask: true,
				})
				uni.requestPayment({
					provider: "wxpay",
					timeStamp: timeStamp,
					nonceStr: nonceStr,
					package: pack,
					signType: signType,
					paySign: paySign,
					success: function(res) {
						that.paySu();
					},
					fail: function(err) {
						that.payFail();
					}
				})
			},

			//支付成功
			paySu() {
				uni.showToast({
					title: '支付成功',
					icon: 'success',
					duration: 2000,
					mask: true,
				})

				this.payState = 1
				this.title = "支付成功"
			},
			//支付失败
			payFail() {
				uni.showModal({
					title: "提示",
					content: "支付失败，请重新下单"
				})
				this.payState = 2
				this.title = "支付失败"
			}
		}
	}
</script>

<style>
	
</style>
